<template>
  <div class="result-container">
    <my-scroll ref="searchScroll">
      <div class="outter">
        <div class="inner">
          <div class="el-input el-input--small el-input--prefix">
            <input
              v-model.lazy="keyword"
              type="text"
              autocomplete="off"
              placeholder="输入关键字，点击回车键即可搜索"
              class="el-input__inner"
            />
            <span class="el-input__prefix">
              <i class="el-input__icon el-icon-search"></i>
            </span>
          </div>
          <div class="title-wrap">
            <span class="title">关键词：{{ keyword }}</span>
            <span class="sub-title">找到{{ total }}个结果</span>
          </div>
          <el-tabs v-model="activeIndex">
            <el-tab-pane label="歌曲" name="songs">
              <table class="el-table">
                <thead>
                  <th class="sort"></th>
                  <th>音乐标题</th>
                  <th>歌手</th>
                  <th>专辑</th>
                  <th class="time">时长</th>
                </thead>
                <tbody>
                  <tr
                    class="el-table__row"
                    v-for="(item, index) in songs"
                    :key="index"
                    @click="playmusic(item)"
                  >
                    <td style="width:50px" class="td_sort">{{ index + 1 }}</td>
                    <td style="width:200px">
                      <div class="song-wrap">
                        <div class="name-wrap">
                          <span>{{ item.name }}</span>
                          <span
                            v-if="item.mvid"
                            class="iconfont icon-mv"
                          ></span>
                        </div>
                      </div>
                    </td>
                    <td style="width:150px">{{ item.artists[0].name }}</td>
                    <td style="width:300px">{{ item.album.name }}</td>
                    <td class="td_time">{{ item.duration | formDate }}</td>
                  </tr>
                </tbody>
              </table>
            </el-tab-pane>
            <el-tab-pane label="歌单" name="lists">
              <div class="recom">
                <div
                  class="recom-item"
                  v-for="(item, index) in lists"
                  :key="index"
                  @click="golist(item.id)"
                >
                  <div class="imgbox">
                    <div class="dsc">
                      <i>播放量：{{ item.playCount | count }}</i>
                    </div>
                    <img :src="item.coverImgUrl" alt />
                    <span class="iconfont icon-play"></span>
                  </div>
                  <div class="listname">{{ item.name }}</div>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="MV" name="mv">
              <div class="items">
                <div
                  class="item"
                  @click="toMv(item.id)"
                  v-for="(item, index) in mvs"
                  :key="index"
                >
                  <div class="img-wrap">
                    <img :src="item.cover" alt />
                    <div class="num-wrap">
                      <div class="iconfont icon-play"></div>
                      <div class="num">{{ item.playCount | count }}</div>
                    </div>
                    <div class="iconbox">
                      <span class="iconfont icon-play"></span>
                    </div>
                  </div>
                  <div class="info-wrap">
                    <div class="name">{{ item.name }}</div>
                    <div class="singer">{{ item.artistName }}</div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
          <div class="pagebox">
            <el-pagination
              @current-change="handleCurrentChange"
              background
              layout="prev, pager, next"
              :total="total"
              :current-page="page"
              :page-size="12"
              hide-on-single-page
            ></el-pagination>
          </div>
          <div class="more" @click="loadMore" v-if="total > 0">
            查看更多
          </div>
        </div>
      </div>
    </my-scroll>
  </div>
</template>

<script>
import { GetSong, Search } from "../../api/music"
export default {
  name: "result",
  data() {
    return {
      activeIndex: "songs",
      keyword: "",
      count: 0,
      limit: 12,
      total: 0,
      type: 1,
      page: 1,
      songs: [],
      lists: [],
      mvs: [],
    }
  },
  filters: {
    formDate(date) {
      var m = parseInt(date / 1000 / 60)
      if (m < 10) {
        m = "0" + m
      }
      var s = parseInt((date / 1000) % 60)
      if (s < 10) {
        s = "0" + s
      }
      return m + ":" + s
    },
    count(playcount) {
      if (playcount >= 10000) {
        playcount = Math.ceil(playcount / 10000) + "万"
      }
      return playcount
    },
  },
  created() {
    console.log("wobeichuajianle ")
    console.log(this.$router.params)
  },
  watch: {
    keyword(newVal) {
      if (newVal) {
        this.page = 1
        this.keyword = newVal
        this.clicksearch()
      }
    },
    activeIndex(newVal) {
      switch (newVal) {
        case "songs":
          this.type = 1
          this.page = 1
          this.clicksearch()
          break
        case "lists":
          this.type = 1000
          this.page = 1
          this.clicksearch()
          break
        case "mv":
          this.type = 1004
          this.page = 1
          this.clicksearch()
          break
      }
    },
  },
  created() {},
  methods: {
    golist(id) {
      this.$router.push(`/musichome/listinfo?id=${id}`)
    },
    toMv(id) {
      this.$router.push(`/musichome/videoinfo?id=${id}`)
    },
    handleCurrentChange(val) {
      this.page = val
      this.clicksearch()
      this.$refs["searchScroll"].$refs["vs"].scrollTo(
        {
          y: "0%",
        },
        0
      )
    },
    // 搜索
    async clicksearch() {
      const res = await Search({
        keywords: this.keyword,
        type: this.type,
        limit: this.limit,
        offset: (this.page - 1) * this.limit,
      })
      if (this.type == 1) {
        this.songs = res.result.songs
        this.total = res.result.songCount
      } else if (this.type == 1000) {
        this.lists = res.result.playlists
        this.total = res.result.playlistCount
      } else {
        this.mvs = res.result.mvs
        this.total = res.result.mvCount
      }
    },
    // 点击播放
    async playmusic(item) {
      this.$store.dispatch("getSong", { id: item.id, autoPlay: true })
    },
    // 移动端加载更多
    async loadMore() {
      this.page++
      const res = await Search({
        keywords: this.keyword,
        type: this.type,
        limit: this.limit,
        offset: (this.page - 1) * this.limit,
      })
      if (this.type == 1) {
        this.songs.push(...res.result.songs)
        this.total = res.result.songCount
      } else if (this.type == 1000) {
        this.lists.push(...res.result.playlists)
        this.total = res.result.playlistCount
      } else {
        this.mvs.push(...res.result.mvs)
        this.total = res.result.mvCount
      }
    },
  },
}
</script>

<style lang="scss" scoped>
@media (max-width: 600px) {
  .result-container {
    width: 100%;
    height: calc(100% - 40px);
    margin: 20px 0px;
    user-select: none;
  }
  .result-container .title-wrap {
    display: flex;
    align-items: flex-end;
    margin: 1.25rem 0.625rem;
    margin-bottom: 30px;
  }
  .el-input {
    width: calc(100% - 1.25rem);
    margin: 0rem 0.625rem;
  }
  .el-tabs {
    margin: 0rem 0.625rem;
  }
  tr td {
    font-size: 0.8rem !important;
  }
  .sort {
    display: none;
  }
  .td_sort {
    display: none;
  }
  .time {
    display: none;
  }
  .td_time {
    display: none;
  }
  .result-container .title-wrap .title {
    font-weight: bold;
    margin-right: 20px;
  }

  .result-container .title-wrap .sub-title {
    font-size: 15px;
    color: #bebebe;
  }
  tr:hover {
    color: rgb(98, 158, 218);
    cursor: pointer;
  }
  .result-container .el-tabs__item {
    font-size: 18px;
  }

  .result-container .el-table td,
  .result-container .el-table th.is-leaf {
    border-bottom: none;
  }
  .pagebox {
    display: none;
  }
  .more {
    width: 100%;
    text-align: center;
    display: block;
    line-height: 1rem;
    padding-top: 0.9rem;
    color: rgba(168, 168, 168, 0.8);
  }
  .recom {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }
  .recom-item {
    width: 46%;
    margin-top: 20px;
    .imgbox {
      width: 100%;
      border-radius: 5px;
      position: relative;
      overflow: hidden;
      img {
        width: 100%;
      }
      .dsc {
        position: absolute;
        width: 100%;
        height: 1.875rem;
        background-color: rgba(0, 0, 0, 0.5);
        top: 0px;
        left: 0px;
        transition: 0.5s;
        text-align: right;
        i {
          display: inline-block;
          width: 85%;
          color: rgb(255, 255, 255);
          font-size: 14px;
          line-height: 25px;
          font-style: normal;
          margin: 0px 10px;
        }
      }

      span {
        position: absolute;
        right: 10px;
        bottom: 10px;
        color: rgb(98, 158, 218);
        width: 35px;
        height: 35px;
        background: rgba(247, 247, 247, 0.5);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        opacity: 0;
        transition: 0.5s;
      }

      span:before {
        transform: translateX(1px);
      }
    }
    .imgbox:hover {
      cursor: pointer;
    }
    .imgbox:hover .dsc {
      top: 0px;
    }
    .imgbox:hover span {
      opacity: 1;
    }
    .listname {
      height: 30px;
      line-height: 30px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .recom-item:hover span {
    opacity: 1;
  }
  .items {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }

  .items .item {
    width: 46%;
    cursor: pointer;
    margin-bottom: 1.25rem;
  }

  .items .item .img-wrap {
    width: 100%;
    position: relative;
    .iconbox {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.6);
      color: rgb(98, 158, 218);
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0;
      transition: 0.5s;
      span {
        color: rgb(98, 158, 218);
        width: 35px;
        height: 35px;
        background: rgba(255, 255, 255, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
      }
      span:hover {
        cursor: pointer;
      }
      span:before {
        transform: translateX(1px);
      }
    }
  }
  .items .item .img-wrap:hover .iconbox {
    opacity: 1;
  }
  .items .item .img-wrap img {
    width: 100%;
    border-radius: 5px;
  }

  .items .item .img-wrap .num-wrap {
    position: absolute;
    color: white;
    top: 0;
    right: 0;
    display: flex;
    align-content: center;
    font-size: 15px;
    padding-right: 5px;
    padding-top: 2px;
  }

  .items .item .img-wrap .num-wrap .icon-play {
    font-size: 12px;
    display: flex;
    align-items: center;
    margin-right: 5px;
  }

  .items .item .info-wrap .name {
    height: 25px;
    line-height: 25px;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .items .item .info-wrap .singer {
    font-size: 14px;
    height: 20px;
    line-height: 20px;
    color: #c5c5c5;
  }
}
@media (min-width: 600px) {
  .result-container {
    width: 100%;
    height: calc(100% - 40px);
    margin: 20px 0px;
    user-select: none;
  }
  .result-container .title-wrap {
    display: flex;
    align-items: flex-end;
    margin: 20px;
    margin-bottom: 30px;
  }
  .el-input {
    width: calc(100% - 40px);
    margin: 0px 20px;
  }
  .el-tabs {
    margin: 0px 20px;
  }
  .result-container .title-wrap .title {
    font-weight: bold;
    margin-right: 20px;
  }

  .result-container .title-wrap .sub-title {
    font-size: 15px;
    color: #bebebe;
  }
  tr:hover {
    color: rgb(98, 158, 218);
    cursor: pointer;
  }
  .result-container .el-tabs__item {
    font-size: 18px;
  }

  .result-container .el-table td,
  .result-container .el-table th.is-leaf {
    border-bottom: none;
  }
  .pagebox {
    margin-top: 20px;
    display: flex;
    width: 100%;
    height: 60px;
    justify-content: center;
    align-items: center;
  }
  .more {
    display: none;
  }
  .recom {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
  }
  .recom-item {
    width: 180px;
    height: 210px;
    margin-top: 20px;
    .imgbox {
      width: 180px;
      height: 180px;
      background: rgb(59, 144, 214);
      border-radius: 5px;
      position: relative;
      overflow: hidden;
      img {
        width: 180px;
      }
      .dsc {
        position: absolute;
        width: 100%;
        height: 30px;
        background-color: rgba(0, 0, 0, 0.5);
        top: 0px;
        left: 0px;
        transition: 0.5s;
        text-align: right;
        i {
          display: inline-block;
          width: 85%;
          color: rgb(255, 255, 255);
          font-size: 14px;
          line-height: 25px;
          font-style: normal;
          margin: 0px 10px;
        }
      }

      span {
        position: absolute;
        right: 10px;
        bottom: 10px;
        color: rgb(98, 158, 218);
        width: 35px;
        height: 35px;
        background: rgba(247, 247, 247, 0.5);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        opacity: 0;
        transition: 0.5s;
      }

      span:before {
        transform: translateX(1px);
      }
    }
    .imgbox:hover {
      cursor: pointer;
    }
    .imgbox:hover .dsc {
      top: 0px;
    }
    .imgbox:hover span {
      opacity: 1;
    }
    .listname {
      height: 30px;
      line-height: 30px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .recom-item:hover span {
    opacity: 1;
  }
  .items {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .items .item {
    width: 250px;
    cursor: pointer;
    margin-right: 20px;
    margin-bottom: 30px;
  }

  .items .item .img-wrap {
    width: 100%;
    position: relative;
    .iconbox {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.6);
      color: rgb(98, 158, 218);
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0;
      transition: 0.5s;
      span {
        color: rgb(98, 158, 218);
        width: 35px;
        height: 35px;
        background: rgba(255, 255, 255, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
      }
      span:hover {
        cursor: pointer;
      }
      span:before {
        transform: translateX(1px);
      }
    }
  }
  .items .item .img-wrap:hover .iconbox {
    opacity: 1;
  }
  .items .item .img-wrap img {
    width: 100%;
    border-radius: 5px;
  }

  .items .item .img-wrap .num-wrap {
    position: absolute;
    color: white;
    top: 0;
    right: 0;
    display: flex;
    align-content: center;
    font-size: 15px;
    padding-right: 5px;
    padding-top: 2px;
  }

  .items .item .img-wrap .num-wrap .icon-play {
    font-size: 12px;
    display: flex;
    align-items: center;
    margin-right: 5px;
  }

  .items .item .info-wrap .name {
    height: 25px;
    line-height: 25px;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .items .item .info-wrap .singer {
    font-size: 14px;
    height: 20px;
    line-height: 20px;
    color: #c5c5c5;
  }
  .outter {
    width: 100%;
    display: flex;
    justify-content: center;
    .inner {
      width: 100%;
      max-width: 1200px;
    }
  }
}
</style>
